package com.pfizer.pfizerExpertForPad;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.pfizer.DB.DataHelper;
import com.pfizer.Entity.pNews;
import com.pfizer.Utils.NoticeBoardListAdapter;
/**
 * 
 * NoticeBoardActivity--<b>公佈欄介面</b>
 * @author krui
 *@version 1.2 
 *
 */
public class NoticeBoardActivity extends Activity implements OnItemClickListener{
	// 全局变量设置---begin--
	Context mContext = null;
	DataHelper dHelper = null;
	pfizerApplication pApplication = null;
	
	List<Map<String, Object>> lmNews = null;
	ListView lvNews = null;
	NoticeBoardListAdapter laNews = null;
	Button btnBack = null;
	TextView tvNoticeBoard = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = NoticeBoardActivity.this;
		setContentView(R.layout.noticeboardlayout);
		// 初始化全局变量--begin--
		InitApp initApp = new InitApp(mContext);
		initApp.SetApp(0);
		pApplication = (pfizerApplication) getApplicationContext();
		dHelper = pApplication.getDataHelper();

		lvNews=(ListView)this.findViewById(R.id.lvNews);
		lvNews.setOnItemClickListener(this);
		tvNoticeBoard=(TextView)this.findViewById(R.id.tvNoticeBoard);
		btnBack=(Button)this.findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}});
		setNewsList();
	}
	/**
	 * 設置公佈欄數據
	 */
	private void setNewsList() {
		//從數據庫中獲取同步好的數據
		lmNews = dHelper.getNewsMapList();
		if (lmNews.size() > 0) {
			//綁定到listview顯示數據
			laNews = new NoticeBoardListAdapter(lmNews, this);
			lvNews.setAdapter(laNews);
			laNews.setSelectItem(0);
			laNews.notifyDataSetChanged();
			String nID = lmNews.get(0).get("ID").toString();
			setNewsCont(nID);
		}
		else
		{
			Toast.makeText(mContext, "目前暫無資料!", 1).show();
		}
	}
	private void setNewsCont(String _ID)
	{
		pNews pn=dHelper.getNewsByID(new Long(_ID).longValue());
		tvNoticeBoard.setText(pn.getCont());
		pn=null;
	}
	
	@Override
    //监听退出键
    public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
    	 finish();
     }
     return super.onKeyDown(keyCode, event);
    }
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		int id = arg0.getId();

		switch (id) {
		case R.id.lvNews:
			laNews.setSelectItem(arg2);
			String nID = lmNews.get(arg2).get("ID").toString();
			setNewsCont(nID);
			laNews.notifyDataSetChanged(); // 该方法内部实现了在每个观察者上面调用onChanged事件。每当发现数据集有改变的情况，或者读取到数据的新状态时，就会调用此方法
			break;
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		mContext = null;
		dHelper = null;
		pApplication = null;
		lmNews = null;
		lvNews = null;
		laNews = null;
		btnBack = null;
		tvNoticeBoard = null;
	}
	
	
}
